/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jfonia.notation;

import org.jfonia.connect5.basics.BasicValueNode;
import org.jfonia.connect5.basics.MutableValueNode;
import org.jfonia.connect5.relations.Equal;
import org.jfonia.model.elements.Beat;
import org.jfonia.model.elements.Clef;
import org.jfonia.model.elements.IStaffElement;
import org.jfonia.model.elements.MusicalElement;
import org.jfonia.model.elements.Tone;
import org.jfonia.pitch.Base40;
import org.jfonia.pitch.RankNodes;

/**
 *
 * @author Rik Bauwens
 */
public class NoteDeprecated extends MusicalElement implements IStaffElement
{
        private StaffElementSequence parentSequence;
        private Tone tone;
        private Beat beat;
        private int currentDuration;

        private RankNodes rankNodes;

        private MutableValueNode<Integer> noteHeadNode;
        private MutableValueNode<Integer> stemNode;
        private MutableValueNode<Integer> flagNode;
        private MutableValueNode<Integer> dotNode;

        public NoteDeprecated(int begin, int duration, Tone tone, StaffElementSequence parentSequence)
        {
                super(duration);
                currentDuration = duration;
                setBegin(begin);
                setTone(tone);
                this.parentSequence = parentSequence;
                noteHeadNode = new BasicValueNode<Integer>(duration);
                stemNode = new BasicValueNode<Integer>(duration);
                new Equal<Integer>(noteHeadNode, stemNode);
                flagNode = new BasicValueNode<Integer>(0);
                dotNode = new BasicValueNode<Integer>(0);
                /*difference.addObserver(new Observer()
                {
                        public void onNotify(Object source)
                        {
                                getTone().setDifference(getTone().getDifference() + difference.getValue() - currentDuration);
                        }
                });*/
        }
        
        private Tone getTone()
        {
                return tone;
        }

        private NoteDeprecated setTone(Tone tone)
        {
                this.tone = tone;
                if(!isRest())
                        rankNodes = new RankNodes(tone.getBase40RankNode());
                return this;
        }

        public NoteDeprecated setBeat(Beat beat)
        {
                this.beat = beat;
                return this;
        }

        public NoteDeprecated setClef(Clef clef)
        {
                rankNodes.setCentralCRankNode(clef.getCentralCRankNode());
                return this;
        }

        public int getNoteHead()
        {
                return noteHeadNode.getValue();
        }

        public int getStem()
        {
                return stemNode.getValue();
        }

        public int getFlag()
        {
                return flagNode.getValue();
        }

        public int getDot()
        {
                return dotNode.getValue();
        }

        public MutableValueNode<Integer> getNoteHeadNode()
        {
                return noteHeadNode;
        }

        public MutableValueNode<Integer> getStemNode()
        {
                return stemNode;
        }

        public MutableValueNode<Integer> getFlagNode()
        {
                return flagNode;
        }

        public MutableValueNode<Integer> getDotNode()
        {
                return dotNode;
        }

        public boolean isRest()
        {
                return tone.getBase40Rank() == null;
        }

        public int getRank()
        {
                return Base40.toMusicalInterval(tone.getBase40Rank()).getRank();
        }

        public int getStaffRank()
        {
                return rankNodes.getRankNode().getValue();
        }

        public MutableValueNode<Integer> getRankNode()
        {
                return rankNodes.getRankNode();
        }

        public MutableValueNode<Integer> getAccidentalNode()
        {
                return rankNodes.getAccidentalNode();
        }

        public void remove()
        {
                tone.setDifference(tone.getDifference() - noteHeadNode.getValue());
                //if(tone.getDifference() == 0)
                        //parentSequence.remove(tone);
        }

        @Override
        public NoteDeprecated clone()
        {
                NoteDeprecated note = new NoteDeprecated(getBegin(), getDifference(), tone, parentSequence);
                //chord.setChord(chordNode.getValue());
                return note;
        }
}
